package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.TreeProblem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author tzp
 * @since 2020/10/12
 */
public class LC144 implements TreeProblem {
    public List<Integer> preorderTraversal(TreeNode root) {
        if (root == null) return Collections.emptyList();
        List<Integer> result = new ArrayList<>();
        LinkedList<TreeNode> linkedList = new LinkedList<>();
        linkedList.addLast(root);
        while (linkedList.size() != 0) {
            TreeNode cur = linkedList.pollLast();
            result.add(cur.val);
            if (cur.right != null) linkedList.addLast(cur.right);
            if (cur.left != null) linkedList.addLast(cur.left);
        }
        return result;
    }

    public static void main(String[] args) {
        Integer[] a = {1, 2, 4, 3, 5, 7, 10, 6, null, 8, null, 11, 9, 15, null, null, null, 12, null, null, null, 13, null, null, null, null, 14};
        TreeNode treeNode = TreeNode.arrayToTreeNode(a);
        System.out.println(new LC144().preorderTraversal(treeNode));
    }
}
